/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.qp.smsmarketing.dao;

import com.qp.smsmarketing.dao.abstractdao.AbstractDAO;
import com.qp.smsmarketing.dao.abstractdao.Hib;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author waldenilson
 */
public class UsuarioDAO extends AbstractDAO {

    public UsuarioDAO()
    {
        super.entityManager = Hib.getSessionFactory();
    }

    public List buscarTodos() {
        return findAllRows("Usuario");
    }

    public boolean salvarObj(Object o) {
        return salvar(o);
    }

    public boolean atualizarDados(Object o) {
        return atualizar(o);
    }

     public Object buscarRegistro(int id) {
        return findOne("Usuario", id);
    }

     public boolean verifyRegistroByLog(String log) {
        query = entityManager.createQuery("SELECT e FROM Usuario e WHERE e.logUsuario = :log");
        query.setParameter("log", log);
        if(query.getResultList().isEmpty())
            return false;
        else
            return true;
    }

    public Object buscarRegistroLogSenha(String log,String senha) {
        return findOneLogSenha("Usuario", log,senha);
    }

    public boolean deletarOperacoesUsuarioByUsuario(int idUsuario) {
        entityManager.getTransaction().begin();
        String sqlDelete = "DELETE FROM OperacaoUsuario where usuario_operacao_usuario = "+idUsuario;

        try {
            entityManager.createQuery(sqlDelete).executeUpdate();
            entityManager.getTransaction().commit();
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }
}
